<template>
    <el-container>
        <el-header class="adminViewHeader">
            <div style="font-weight:bold;font-size: 25px;color: #2b73af;">管理员界面-用户管理</div>
            <div class="header-right">
                <el-input v-model="searchValue" placeholder="请输入搜索关键字" class="input"></el-input>
                <el-button type="primary" @click="searchUser(searchValue)" class="selectBtn">搜索</el-button>
            </div>

            <div class="header-left">
                <el-button @click="goToPostPage" style="margin-right: 20px;">发帖</el-button>
                <el-dropdown trigger="click" class="dropdown">
                    <el-avatar style="margin-top: 25px; background-color: RGB(64,158,255);"> {{ userName }} </el-avatar>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item>
                            <el-button-text @click="goToUserInformation">我的空间</el-button-text>
                        </el-dropdown-item>
                        <el-dropdown-item>
                            <el-button-text @click="goToadminMain">帖子管理</el-button-text>
                        </el-dropdown-item>
                        <el-dropdown-item>
                            <el-button-text @click="goToLogin">登出</el-button-text>
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>

        </el-header>


        <el-container style="height: 100vh;">
            <el-aside width="200px" style="height: 100%; background-color: #f7f7f7">
                <el-menu default-active="2" class="el-menu-vertical-demo" background-color="#f7f7f7" text-color="#000000"
                    active-text-color="#2775b6">
                    <el-menu-item>
                        <span style="font-weight:bold;font-size: 20px;">whx论坛系统</span>
                    </el-menu-item>

                    <el-menu-item @click="showAllUser">
                        <i class="el-icon-notebook-2"></i>
                        <span slot="title">所有用户</span>
                    </el-menu-item>

                    <el-menu-item @click="showAllBanUser">
                        <i class="el-icon-notebook-2"></i>
                        <span slot="title">封禁用户</span>
                    </el-menu-item>
                </el-menu>
            </el-aside>

            <el-main>
                <el-table v-loading="loading" v-if="showAllUsers" :data="showData">
                    <el-table-column prop="userId" label="用户编号" width="200"></el-table-column>
                    <el-table-column prop="userName" label="用户名" width="200"></el-table-column>
                    <el-table-column prop="role" label="角色" width="200">
                        <template slot-scope="scope">
                            {{ Role[scope.row.role - 1] }}
                        </template>
                    </el-table-column>
                    <el-table-column width="120">
                        <template slot-scope="scope">
                            <el-button @click="deleteUser(scope.row.userId)" type="danger" round>注销</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column width="125">
                        <template slot-scope="scope">
                            <el-button @click="showBanDialog(scope.row.userId)" type="warning" round>封禁</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column width="200">
                        <template slot-scope="scope">
                            <el-button @click="authorization(scope.row.userId)" type="success" round>授予管理员权限</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column width="200">
                        <template slot-scope="scope">
                            <el-button @click="transferAuthorization(scope.row.userId)" type="info"
                                round>转移管理员权限</el-button>
                        </template>
                    </el-table-column>
                </el-table>


                <el-table v-loading="loading" v-if="!showAllUsers" :data="showData">
                    <el-table-column prop="userId" label="用户编号" width="200"></el-table-column>
                    <el-table-column prop="adminId" label="封禁管理员编号" width="200"></el-table-column>
                    <el-table-column prop="reason" label="原因" width="280"></el-table-column>
                    <el-table-column prop="startTime" label="开始时间" width="230"></el-table-column>
                    <el-table-column prop="endTime" label="结束时间" width="230"></el-table-column>
                    <el-table-column width="120">
                        <template slot-scope="scope">
                            <el-button @click="DeleteBan(scope.row.userId)" type="primary" plain>解封</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-main>
        </el-container>

        <el-dialog title="封禁用户" :visible.sync="banDialogVisible" :before-close="handleBanDialogClose">
            <el-form :model="banFormData" ref="banForm" :rules="banFormRules" label-width="80px">
                <el-form-item label="时长(M)" prop="duration">
                    <el-input v-model.number="banFormData.duration" type="number"></el-input>
                </el-form-item>
                <el-form-item label="原因" prop="duration">
                    <el-input v-model.number="banFormData.reason" type="text"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="banDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="banUser1">确认</el-button>
            </div>
        </el-dialog>

    </el-container>
</template>
  
<script>
export default {
    data() {
        return {
            searchValue: "",
            // user: [],
            // banUser: [],
            Role: ['Admin', 'User'],
            showData: [],
            banDialogVisible: false,
            showAllUsers: true,
            loading: true,
            banUserId: '',
            banFormData: {
                duration: 0,
                reason: ''
            },
            banFormRules: {
                duration: [
                    { required: true, message: "请输入封禁时长", trigger: "blur" }
                ]
            }
        }
    }, async created() {
        this.userId = this.$route.params.userId;
        this.userName = this.$route.params.userName;
        await this.$axios.get('https://localhost:44300/api/User/getallUser')
            .then(response => {
                this.showData = this.user = response.data.response;
                this.$forceUpdate();
                this.loading = false
            })
            .catch(error => {
                console.error(error);
            });

        await this.$axios.get('https://localhost:44300/api/BanRecord/getBanUser')
            .then(response => {
                this.banUser = response.data.response;
                this.$forceUpdate();
                this.loading = false
            })
            .catch(error => {
                console.error(error);
            });
    },
    methods: {
        deleteUser(userId) {
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.get('https://localhost:44300/api/User/DeleteUser', {
                    params: {
                        id: userId
                    }
                })
                    .then(response => {
                        if (response.data.success === true) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.loadData();
                        } else {
                            this.$message({
                                message: response.data.msg,
                                type: 'error'
                            });
                        }
                    })
                    .catch(error => {
                        console.error(error);
                    });
            }).catch(() => {
                // 取消删除，不执行任何操作
            });
        },
        goToSearchPageAdmin() {
            this.$router.push("/SearchPageAdmin");
        },
        goToSearchPage() {
            if (this.searchValue.trim() === '') {
                // 如果搜索框为空则不跳转到搜索页面
                return;
            }
            this.$router.push('/searchPage')
            // 根据 this.searchValue 进行搜索
        },
        goToLogin() {
            this.$router.push("/login");
        },
        goToadminMain() {
            this.$router.push({ name: 'adminMain', params: { userId: this.userId, userName: this.userName } })
        },
        goToUserInformation() {
            this.$router.push({ name: 'userInformation', params: { userId: this.userId, userName: this.userName, backPage: 'adminMain' } })
        },
        goToPostPage() {
            this.$router.push({ name: 'postPage', params: { userId: this.userId, userName: this.userName, backPage: 'adminChargeUser' } })
        },
        showAllUser() {
            this.loadData();
            // this.showData = this.user;
            this.showAllUsers = true;
        },
        showAllBanUser() {
            this.loadBanData();
            // this.showData = this.banUser;
            this.showAllUsers = false;
        },
        showBanDialog(userId) {
            this.banFormData.duration = 0;
            this.banFormData.reason = '';
            this.banDialogVisible = true;
            this.banUserId = userId;
        },
        // 关闭封禁对话框时清空表单数据
        handleBanDialogClose(done) {
            if (done) {
                this.$refs.banForm.resetFields();
            }
        },
        // 发送请求封禁用户
        banUser1() {
            this.$axios.get('https://localhost:44300/api/BanRecord/Ban', {
                params: {
                    adminId: this.userId,
                    userId: this.banUserId,
                    reason: this.banFormData.reason,
                    time: this.banFormData.duration
                }
            })
                .then(response => {
                    if (response.data.success === true) {
                        this.$message({
                            message: '封禁成功',
                            type: 'success'
                        });
                        this.banDialogVisible = false
                    } else {
                        this.$message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    }
                })
                .catch(error => {
                    console.error(error);
                });
        },
        authorization(userId) {
            this.$axios.get('https://localhost:44300/api/User/authorization', {
                params: {
                    AdminId: this.userId,
                    UserId: userId,
                }
            })
                .then(response => {
                    if (response.data.success === true) {
                        this.$message({
                            message: '授权成功',
                            type: 'success'
                        });
                        this.loadData();
                    } else {
                        this.$message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    }
                })
                .catch(error => {
                    console.error(error);
                });
        },
        transferAuthorization(userId) {
            this.$axios.get('https://localhost:44300/api/User/transferAuthorization', {
                params: {
                    AdminId: this.userId,
                    UserId: userId,
                }
            })
                .then(response => {
                    if (response.data.success === true) {
                        this.$message({
                            message: '授权成功',
                            type: 'success'
                        });
                        this.loadData();
                    } else {
                        this.$message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    }
                })
                .catch(error => {
                    console.error(error);
                });
        },
        DeleteBan(userId) {
            this.$axios.get('https://localhost:44300/api/BanRecord/DeleteBan', {
                params: {
                    UserId: userId,
                }
            })
                .then(response => {
                    if (response.data.success === true) {
                        this.$message({
                            message: '解封成功',
                            type: 'success'
                        });
                        this.loadBanData();
                    } else {
                        this.$message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    }
                })
                .catch(error => {
                    console.error(error);
                });
        },
        loadData() {
            this.$axios.get('https://localhost:44300/api/User/getallUser')
                .then(response => {
                    this.showData = response.data.response;
                    this.$forceUpdate();
                })
                .catch(error => {
                    console.error(error);
                });
        },
        loadBanData() {
            this.$axios.get('https://localhost:44300/api/BanRecord/getBanUser')
                .then(response => {
                    this.showData = response.data.response;
                    this.$forceUpdate();
                })
                .catch(error => {
                    console.error(error);
                });
        }, searchUser(searchValue) {
            this.$axios.get('https://localhost:44300/api/User/selectUserByName', {
                params: {
                    username: searchValue
                }
            })
                .then(response => {
                    this.showData = response.data.response;
                    this.showAllUsers = true;
                    this.searchValue = '';
                })
                .catch(error => {
                    console.error(error);
                });
        }
    },
};
</script>
  
<style scoped>
.adminViewHeader {
    line-height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #f7f7f7;
    box-shadow: 0 0 5px #f7f7f7;
}

.header-left {
    display: flex;
    align-items: center;

}

.header-right {
    display: flex;
    align-items: center;
    margin: auto;
}

.dropdown {
    margin-right: 20px;
}

.input {
    width: 350px;
    margin-right: 10px;
}

.selectBtn {
    width: 100px;
}

.post-list {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.post-card {
    width: 80%;
    margin-bottom: 16px;
    cursor: pointer;
}

.post-title {
    font-size: 24px;
    margin-bottom: 8px;
}

.post-info {
    font-size: 14px;
    color: #888;
    margin-bottom: 8px;
}

.post-category {
    margin-right: 8px;
}

.post-summary {
    font-size: 16px;
    line-height: 1.5;
}

.table-cell-content {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
</style>
  